package com.stox.ui.javafx.core.util;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import com.stox.util.PathUtil;

public class MessageSource {

	/**
	 * TODO get resource bundle according to user preferred locale, not default
	 */
	private static ResourceBundle messages ;
	private static final MessageFormat messageFormat = new MessageFormat("");
	
	static{
		try{
			String configurationPath = PathUtil.getRootPath()+"resource"+File.separator+"message";
			File file = new File(configurationPath);
			if(!file.exists()){
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			URL[] urls = {file.toURI().toURL()};
			ClassLoader classLoader = new URLClassLoader(urls);
			messages = ResourceBundle.getBundle("messages", Locale.getDefault(), classLoader);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static String getMessage(String key){
		return messages.getString(key);
	}
	
	public static String getMessage(String key, Object...args){
		messageFormat.applyPattern(getMessage(key));
		return messageFormat.format(args);
	}
	
}
